package wnhz.com.business.forget;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

public class ForgetPasswordResetActivity extends BaseActivity implements View.OnClickListener, ActionBarClickListener {
    @BindView(R.id.iv_password)
    ImageView iv_password;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.ed_passwordNew)
    EditText ed_passwordNew;
    @BindView(R.id.bt_sure)
    Button bt_sure;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;

    private String phone;
    private boolean isHidden = true;
    private String code;
    private Context context;

    public static Intent createIntent(Context context, String code, String phone) {
        Intent intent = new Intent(context, ForgetPasswordResetActivity.class);
        intent.putExtra("code", code);
        intent.putExtra("phone", phone);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
        code = intent.getStringExtra("code");
        phone = intent.getStringExtra("phone");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_forget_password_reset);
        ButterKnife.bind(this);
        parseIntent();
        actionbar.setData("设置新密码", R.drawable.ic_lefft_white, null, 0, null, 0,this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        initView();
    }

    private void initView() {
        iv_password.setOnClickListener(this);
        bt_sure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //密码显示
            case R.id.iv_password:
                if (isHidden) {
                    //设置EditText文本为可见的
//                    iv_password.setImageResource(R.drawable.ic_login_look_selected2x);
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //设置EditText文本为隐藏的
//                    iv_password.setImageResource(R.drawable.ic_login_look_unselected2x);
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                isHidden = !isHidden;
                et_password.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence charSequence = et_password.getText();
                if (charSequence instanceof Spannable) {
                    Spannable spanText = (Spannable) charSequence;
                    Selection.setSelection(spanText, charSequence.length());
                }
                break;
            case R.id.bt_sure:
                String passwordNew = ed_passwordNew.getText().toString();
                String password = et_password.getText().toString();
                if (passwordNew.equals(password)) {
                    if (password.length() >= 6) {
                        upYanZhengYanSure();
                    } else {
                        MyToast("密码不能少于6位");
                    }
                } else {
                    MyToast("两次密码不一致");
                }

                break;
            default:
                break;
        }

    }

    private void upYanZhengYanSure() {
        SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String token = sp.getString("token", "");
        String repassword = ed_passwordNew.getText().toString();
        String password = et_password.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("mobile",phone);
        map.put("code", code);
        map.put("password", password);
        map.put("repassword", repassword);

        XUtil.Post(Url.USER_FORGOTPASSWORD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    String info = jsonObject.optString("info");
                    if ("1".equals(re)) {


                        finish();
                    }
                    MyToast(info);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
}
